package com.panda.system.service.impl;

import com.panda.common.core.domain.AjaxResult;
import com.panda.common.utils.DateUtils;
import com.panda.system.common.constant.EnumConstants;
import com.panda.system.domain.PandaTaskDetail;
import com.panda.system.domain.TPandaEvalutionTask;
import com.panda.system.mapper.TPandaEvalutionTaskMapper;
import com.panda.system.service.IPandaTaskDetailService;
import com.panda.system.service.ITPandaEvalutionTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 评价任务看板Service业务层处理
 * 
 * @author panda
 * @date 2021-02-28
 */
@Service
@Transactional
public class TPandaEvalutionTaskServiceImpl implements ITPandaEvalutionTaskService 
{
    @Autowired
    private TPandaEvalutionTaskMapper tPandaEvalutionTaskMapper;

    @Autowired
    private IPandaTaskDetailService taskDetailService;

    /**
     * 查询评价任务看板
     * 
     * @param id 评价任务看板ID
     * @return 评价任务看板
     */
    @Override
    public TPandaEvalutionTask selectTPandaEvalutionTaskById(Long id)
    {
        return tPandaEvalutionTaskMapper.selectTPandaEvalutionTaskById(id);
    }

    /**
     * 查询评价任务看板列表
     * 
     * @param tPandaEvalutionTask 评价任务看板
     * @return 评价任务看板
     */
    @Override
    public List<TPandaEvalutionTask> selectTPandaEvalutionTaskList(TPandaEvalutionTask tPandaEvalutionTask)
    {
        Calendar calendar = Calendar.getInstance();
        if("oneMonth".equals(tPandaEvalutionTask.getDateSuchType())){
            tPandaEvalutionTask.setStartTime(new Date());
            //过去一月
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR, -30);
            Date m = calendar.getTime();
            tPandaEvalutionTask.setEndTime(m);
        }
        if("twoMonth".equals(tPandaEvalutionTask.getDateSuchType())){
            tPandaEvalutionTask.setStartTime(new Date());
            //过去一月
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR, -60);
            Date m = calendar.getTime();
            tPandaEvalutionTask.setEndTime(m);
        }
        return tPandaEvalutionTaskMapper.selectTPandaEvalutionTaskList(tPandaEvalutionTask);
    }

    /**
     * 新增评价任务看板
     * 
     * @param tPandaEvalutionTask 评价任务看板
     * @return 结果
     */
    @Override
    public int insertTPandaEvalutionTask(TPandaEvalutionTask tPandaEvalutionTask)
    {
        tPandaEvalutionTask.setCreateTime(DateUtils.getNowDate());
        return tPandaEvalutionTaskMapper.insertTPandaEvalutionTask(tPandaEvalutionTask);
    }

    /**
     * 修改评价任务看板
     * 
     * @param tPandaEvalutionTask 评价任务看板
     * @return 结果
     */
    @Override
    public int updateTPandaEvalutionTask(TPandaEvalutionTask tPandaEvalutionTask)
    {
        tPandaEvalutionTask.setUpdateTime(DateUtils.getNowDate());
        return tPandaEvalutionTaskMapper.updateTPandaEvalutionTask(tPandaEvalutionTask);
    }

    /**
     * 批量删除评价任务看板
     * 
     * @param ids 需要删除的评价任务看板ID
     * @return 结果
     */
    @Override
    public int deleteTPandaEvalutionTaskByIds(Long[] ids)
    {
        return tPandaEvalutionTaskMapper.deleteTPandaEvalutionTaskByIds(ids);
    }

    /**
     * 删除评价任务看板信息
     * 
     * @param id 评价任务看板ID
     * @return 结果
     */
    @Override
    public int deleteTPandaEvalutionTaskById(Long id)
    {
        return tPandaEvalutionTaskMapper.deleteTPandaEvalutionTaskById(id);
    }

    @Override
    public AjaxResult createAfterEvaluationTodo(TPandaEvalutionTask evalutionTask) {
        if(evalutionTask ==null || evalutionTask.getId() ==null){
            return AjaxResult.error("生成评价待办失败，任务Id为空！");
        }
        evalutionTask = tPandaEvalutionTaskMapper.selectTPandaEvalutionTaskById(evalutionTask.getId());
        PandaTaskDetail pandaTaskDetail = taskDetailService.selectPandaTaskDetailById(Long.valueOf(evalutionTask.getTaskDetailId()));
        if(pandaTaskDetail ==null || pandaTaskDetail.getTaskDetailId() ==null){
            return AjaxResult.error("生成评价待办失败，任务完成明细不存在！");
        }
        // 修改评价待办状态
        evalutionTask.setStatus(EnumConstants.EvalutionTaskStatus.BE_SEND.getCode());
        tPandaEvalutionTaskMapper.updateTPandaEvalutionTask(evalutionTask);

        // 判断是否已生成
        TPandaEvalutionTask tPandaEvalutionTask  = new TPandaEvalutionTask();
        tPandaEvalutionTask.setResponsibleUserId(pandaTaskDetail.getResponsibleUserId());
        tPandaEvalutionTask.setTaskDetailId(String.valueOf(pandaTaskDetail.getTaskId()));
        tPandaEvalutionTask.setEvalutionType(1);
        List<TPandaEvalutionTask> tPandaEvalutionTasks = tPandaEvalutionTaskMapper.selectTPandaEvalutionTaskList(tPandaEvalutionTask);
        if(!CollectionUtils.isEmpty(tPandaEvalutionTasks)){
            return AjaxResult.error("生成评价待办失败，待办已存在！");
        }

        TPandaEvalutionTask newPandaEvalutionTask  = new TPandaEvalutionTask();
        newPandaEvalutionTask.setTaskDetailId(String.valueOf(pandaTaskDetail.getTaskDetailId()));
        newPandaEvalutionTask.setResponsibleUserId(pandaTaskDetail.getResponsibleUserId());
        newPandaEvalutionTask.setStatus(EnumConstants.SysStatus.OPEN.getCode());

        newPandaEvalutionTask.setEvalutionType(1);
        tPandaEvalutionTaskMapper.insertTPandaEvalutionTask(newPandaEvalutionTask);

        return AjaxResult.success();
    }
}
